package com.prs.crm.action.oa;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.MyMailList;
import com.prs.crm.domain.oa.MyMailListType;
import com.prs.crm.domain.oa.Notice;
import com.prs.crm.service.oa.MyMailListService;
import com.prs.crm.service.oa.MyMailListTypeService;

public class MyMailListTypeAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private PaginationSupport pagination;
	private Integer[] ids;
	private MyMailListType type;
	private MyMailListTypeService myMailListTypeService;
	private MyMailListService myMailListService;
	
	@Action("listMyMailListType")
	public String listMyMailListType(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MyMailListType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMyMailListTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("editMyMailListType")
	public String editMyMailListType(){
		return SUCCESS;
	}
	
	@Action(value = "removeMyMailListType", results = {
			@Result(name = "success", type = "redirect", location = "listMyMailListType"),
			@Result(name = "error", type = "chain", location = "listMyMailListType") })
	public String removeMyMailListType() {
		if (ids == null) {
			this.addActionError("请选择要删除的通讯录类别");
			return ERROR;
		}
		for (Integer id : ids) {
			MyMailListType type = this.getMyMailListTypeService().get(id);
			if (this.getMyMailListService().getMyMailListByType(type).size() != 0) {
				this.addActionError("该类型包含数据,无法删除");
				return ERROR;
			}
		}
		this.getMyMailListTypeService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Action(value = "saveMyMailListType", results = {
			@Result(name = "success", type = "redirect", location = "listMyMailListType"),
			@Result(name = "error", type = "chain", location = "editMyMailListType") })
	public String saveMyMailListType() {
		if(type.getTitle() == null || type.getTitle().trim().equals("")){
			this.addActionError("请输入类型名");
			return ERROR;
		}
		Person person = AppContext.getAccountContext().currentUser();
		if(this.getMyMailListTypeService().getTypeByTitle(type.getTitle(),person) != null){
			this.addActionError("该类型已存在");
			return ERROR;
		}
		type.setPerson(person);
		this.getMyMailListTypeService().save(type);
		return SUCCESS;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public MyMailListType getType() {
		return type;
	}
	public void setType(MyMailListType type) {
		this.type = type;
	}
	public MyMailListTypeService getMyMailListTypeService() {
		return myMailListTypeService;
	}
	public void setMyMailListTypeService(MyMailListTypeService myMailListTypeService) {
		this.myMailListTypeService = myMailListTypeService;
	}

	public MyMailListService getMyMailListService() {
		return myMailListService;
	}

	public void setMyMailListService(MyMailListService myMailListService) {
		this.myMailListService = myMailListService;
	}

}
